#include "StdAfx.h"
#include "ZLights.h"

ZLights::ZLights(VOID)
{
}

ZLights::~ZLights(VOID)
{
}

VOID ZLights::SetupLights()
{
	D3DMATERIAL9 mtrl;
	ZeroMemory( &mtrl, sizeof( D3DMATERIAL9 ) );
	mtrl.Diffuse.r = GINTERFACE.m_stLight.fMaterialDiffuse[0];
	mtrl.Diffuse.g = GINTERFACE.m_stLight.fMaterialDiffuse[1];
	mtrl.Diffuse.b = GINTERFACE.m_stLight.fMaterialDiffuse[2];
	mtrl.Diffuse.a = GINTERFACE.m_stLight.fMaterialDiffuse[3];

	mtrl.Ambient.r = GINTERFACE.m_stLight.fMaterialAmbiant[0];
	mtrl.Ambient.g = GINTERFACE.m_stLight.fMaterialAmbiant[1];
	mtrl.Ambient.b = GINTERFACE.m_stLight.fMaterialAmbiant[2];
	mtrl.Ambient.a = GINTERFACE.m_stLight.fMaterialAmbiant[3];

	mtrl.Emissive.r = 0.0f;
	mtrl.Emissive.g = 0.0f;
	mtrl.Emissive.b = 0.0f;
	mtrl.Emissive.a = 0.0f;

	mtrl.Power = 0.0f;

	mtrl.Specular.r = 0.0f;
	mtrl.Specular.g = 0.0f;
	mtrl.Specular.b = 0.0f;
	mtrl.Specular.a = 0.0f;


	GD3D9DEVICE->SetMaterial( &mtrl );

	D3DXVECTOR3 vecDir;
	D3DLIGHT9 light;
	ZeroMemory( &light, sizeof( D3DLIGHT9 ) );

	light.Type = static_cast<D3DLIGHTTYPE>(GINTERFACE.m_stLight.iMode);
	light.Diffuse.r = GINTERFACE.m_stLight.fDiffuse[0];
	light.Diffuse.g = GINTERFACE.m_stLight.fDiffuse[1];
	light.Diffuse.b = GINTERFACE.m_stLight.fDiffuse[2];
	light.Diffuse.a = GINTERFACE.m_stLight.fDiffuse[3];

	light.Ambient.r = GINTERFACE.m_stLight.fAmbient[0];
	light.Ambient.g = GINTERFACE.m_stLight.fAmbient[1];
	light.Ambient.b = GINTERFACE.m_stLight.fAmbient[2];
	light.Ambient.a = GINTERFACE.m_stLight.fAmbient[3];

	light.Specular.r = GINTERFACE.m_stLight.fSpecular[0];
	light.Specular.g = GINTERFACE.m_stLight.fSpecular[1];
	light.Specular.b = GINTERFACE.m_stLight.fSpecular[2];
	light.Specular.a = GINTERFACE.m_stLight.fSpecular[3];

	light.Position.x = GINTERFACE.m_stLight.fPos[0];
	light.Position.y = GINTERFACE.m_stLight.fPos[1];
	light.Position.z = GINTERFACE.m_stLight.fPos[2];
	
	light.Range = GINTERFACE.m_stLight.fRange;
	light.Theta = GINTERFACE.m_stLight.fSpecularTheta;
	light.Phi = GINTERFACE.m_stLight.fSpecularPhi;
	light.Falloff = GINTERFACE.m_stLight.fFallOff;
	light.Attenuation0 = GINTERFACE.m_stLight.fAttenuation0;

	vecDir = D3DXVECTOR3( static_cast<FLOAT>(GINTERFACE.m_stLight.fDir[0]), 
						  static_cast<FLOAT>(GINTERFACE.m_stLight.fDir[1]), 
						  -static_cast<FLOAT>(GINTERFACE.m_stLight.fDir[2]) );

	D3DXVec3Normalize( ( D3DXVECTOR3* )&light.Direction, &vecDir );
	
	
	if( GINTERFACE.m_stLight.iMode == ZInterFaceEnum::Light::NONE )
	{
		GD3D9DEVICE->LightEnable( 0, FALSE );
		GD3D9DEVICE->SetRenderState( D3DRS_LIGHTING, FALSE );
		GD3D9DEVICE->SetRenderState( D3DRS_AMBIENT, 0x00000000 );
	}
	else
	{
		GD3D9DEVICE->SetLight( 0, &light );
		GD3D9DEVICE->LightEnable( 0, TRUE );
		GD3D9DEVICE->SetRenderState( D3DRS_LIGHTING, TRUE );
		GD3D9DEVICE->SetRenderState( D3DRS_AMBIENT, 0x00000000 );
	}
}
